package top;

/**
 *
 * 除自身以外数组的乘积
 *
 * @author chenyw
 * @date 2022/7/19 20:02
 */
public class Top238productExceptSelf {
    public static void main(String[] args) {
        int[] res = new Top238productExceptSelf().productExceptSelf(new int[]{1, 2, 3, 4});
        for (int re : res) {
            System.out.println(re);
        }
    }
    public int[] productExceptSelf(int[] nums) {
        int n =nums.length;
        int[] left = new int[n];
        int right = 1;
        left[0] = 1;
        for (int i = 1; i < n; i++) {
            left[i] = left[i - 1] * nums[i - 1];
        }
        for (int i = n - 1; i >= 0; i--) {
            left[i] = left[i] * right;
            right *= nums[i];
        }
        return left;
    }
}
